package com.amikomgamedev.labirinhantu.text;

import java.util.ArrayList;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.util.color.Color;

import android.content.Context;

import com.amikomgamedev.labirinhantu.Utils;
import com.amikomgamedev.labirinhantu.grafik.Grafik_Detach;

public class Text_Manager extends Text_Load implements Text_Data
{
	

	// teks untuk help
	public Text	txt_coin, txt_get, txt_to_swim, txt_press, txt_to_diss, txt_avoid;

	// teks untuk credit
	public Text	project_man, project_man_name, producer, producer_name1, producer_name2, gamedesign, gamedesign_name1,
			gamedesign_name2, gamedesign_name3, programmer, programmer_name1, programmer_name2, artist, artist_name1,
			artist_name2, artist_name3, artist_name4, artist_name5, sound_eng, sound_eng_name1, sound_eng_name2,
			dedicated, dedicated1, dedicated2, dedicated3, event, event_name1, event_name2, copyright, year, version, version_code;
	
//	public Text	txt_Time;
	public Text	txt_Rule;

	public Text	txt_Menu_Stage;
	public Text	txt_Menu_Level;
	public ArrayList<Text> lst_Rule	= new ArrayList<Text>();
	
	private Text	txt_Clue;

	public Text_Manager(Context con, Scene scene, HUD hud, Grafik_Detach graf)
	{
		super(con, scene, hud, graf);
		
	}

//	public void attachTeksHelp(Sprite spr)
//	{
//		Utils.TRACE("pasang teks");
//		if (txt_coin == null)
//		{
//			txt_coin = new Text(0, 0, fnt_Small, TEKS_COIN, getVbo());
//		}
//		spr.attachChild(txt_coin);
//
//		if (txt_get == null)
//		{
//			txt_get = new Text(0, 0, fnt_Small, TEKS_GET, getVbo());
//		}
//		spr.attachChild(txt_get);
//
//		if (txt_to_swim == null)
//		{
//			txt_to_swim = new Text(0, 0, fnt_Small, TEKS_TO_SWIM, getVbo());
//		}
//		spr.attachChild(txt_to_swim);
//
//		if (txt_press == null)
//		{
//			txt_press = new Text(0, 0, fnt_Small, TEKS_PRESS, getVbo());
//		}
//		spr.attachChild(txt_press);
//
//		if (txt_to_diss == null)
//		{
//			txt_to_diss = new Text(0, 0, fnt_Small, TEKS_TO_DISS, getVbo());
//		}
//		spr.attachChild(txt_to_diss);
//
//		if (txt_avoid == null)
//		{
//			txt_avoid = new Text(0, 0, fnt_Small, TEKS_AVOID, getVbo());
//		}
//
//		spr.attachChild(txt_avoid);
//
//		txt_coin.setPosition(Utils.getRatioW(180), Utils.getRatioH(105));
//		txt_get.setPosition(Utils.getRatioW(180), Utils.getRatioH(165));
//		txt_to_swim.setPosition(Utils.getRatioW(320), Utils.getRatioH(165));
//		txt_press.setPosition(Utils.getRatioW(180), Utils.getRatioH(230));
//		txt_to_diss.setPosition(Utils.getRatioW(330), Utils.getRatioH(230));
//		txt_avoid.setPosition(Utils.getRatioW(180), Utils.getRatioH(295));
//	}

	public void attachTeksCredit(HUD hud)
	{

		// float y = RES_HEIGHT - (RES_HEIGHT / 4);

		if (project_man == null)
		{
			project_man = new Text(0, 0, fnt_Medium, TEKS_PROJECT_MANAGER, getVbo());
		}
		hud.attachChild(project_man);

		if (project_man_name == null)
		{
			project_man_name = new Text(0, 0, fnt_Small, TEKS_PROJECT_MANAGER_NAME, getVbo());
		}
		hud.attachChild(project_man_name);

		if (producer == null)
		{
			producer = new Text(0, 0, fnt_Medium, TEKS_PRODUSER, getVbo());
		}
		hud.attachChild(producer);

		if (producer_name1 == null)
		{
			producer_name1 = new Text(0, 0, fnt_Small, TEKS_PRODUSER_NAME1, getVbo());
		}
		hud.attachChild(producer_name1);

		if (producer_name2 == null)
		{
			producer_name2 = new Text(0, 0, fnt_Small, TEKS_PRODUSER_NAME2, getVbo());
		}
		hud.attachChild(producer_name2);

		if (gamedesign == null)
		{
			gamedesign = new Text(0, 0, fnt_Medium, TEKS_GAMEDESIGNER, getVbo());
		}
		hud.attachChild(gamedesign);

		if (gamedesign_name1 == null)
		{
			gamedesign_name1 = new Text(0, 0, fnt_Small, TEKS_GAMEDESIGNER_NAME1, getVbo());
		}
		hud.attachChild(gamedesign_name1);

		if (gamedesign_name2 == null)
		{
			gamedesign_name2 = new Text(0, 0, fnt_Small, TEKS_GAMEDESIGNER_NAME2, getVbo());
		}
		hud.attachChild(gamedesign_name2);

		if (gamedesign_name3 == null)
		{
			gamedesign_name3 = new Text(0, 0, fnt_Small, TEKS_GAMEDESIGNER_NAME3, getVbo());
		}
		hud.attachChild(gamedesign_name3);

		if (programmer == null)
		{
			programmer = new Text(0, 0, fnt_Medium, TEKS_PROGRAMMER, getVbo());
		}
		hud.attachChild(programmer);

		if (programmer_name1 == null)
		{
			programmer_name1 = new Text(0, 0, fnt_Small, TEKS_PROGRAMMER_NAME1, getVbo());
		}
		hud.attachChild(programmer_name1);

		if (programmer_name2 == null)
		{
			programmer_name2 = new Text(0, 0, fnt_Small, TEKS_PROGRAMMER_NAME2, getVbo());
		}
		hud.attachChild(programmer_name2);

		if (artist == null)
		{
			artist = new Text(0, 0, fnt_Medium, TEKS_ARTIST, getVbo());
		}
		hud.attachChild(artist);

		if (artist_name1 == null)
		{
			artist_name1 = new Text(0, 0, fnt_Small, TEKS_ARTIST_NAME1, getVbo());
		}
		hud.attachChild(artist_name1);

		if (artist_name2 == null)
		{
			artist_name2 = new Text(0, 0, fnt_Small, TEKS_ARTIST_NAME2, getVbo());
		}
		hud.attachChild(artist_name2);

		if (artist_name3 == null)
		{
			artist_name3 = new Text(0, 0, fnt_Small, TEKS_ARTIST_NAME3, getVbo());
		}
		hud.attachChild(artist_name3);

		if (artist_name4 == null)
		{
			artist_name4 = new Text(0, 0, fnt_Small, TEKS_ARTIST_NAME4, getVbo());
		}
		hud.attachChild(artist_name4);

		if (artist_name5 == null)
		{
			artist_name5 = new Text(0, 0, fnt_Small, TEKS_ARTIST_NAME5, getVbo());
		}
		hud.attachChild(artist_name5);

		if (sound_eng == null)
		{
			sound_eng = new Text(0, 0, fnt_Medium, TEKS_SOUND_ENGINEER, getVbo());
		}
		hud.attachChild(sound_eng);

		if (sound_eng_name1 == null)
		{
			sound_eng_name1 = new Text(0, 0, fnt_Small, TEKS_SOUND_ENGINEER_NAME1, getVbo());
		}
		hud.attachChild(sound_eng_name1);

		if (sound_eng_name2 == null)
		{
			sound_eng_name2 = new Text(0, 0, fnt_Small, TEKS_SOUND_ENGINEER_NAME2, getVbo());
		}
		hud.attachChild(sound_eng_name2);

		if (dedicated == null)
		{
			dedicated = new Text(0, 0, fnt_Medium, TEKS_DEDICATED, getVbo());
		}
		hud.attachChild(dedicated);

		if (dedicated1 == null)
		{
			dedicated1 = new Text(0, 0, fnt_Small, TEKS_DEDICATED_NAME1, getVbo());
		}
		hud.attachChild(dedicated1);

		if (dedicated2 == null)
		{
			dedicated2 = new Text(0, 0, fnt_Small, TEKS_DEDICATED_NAME2, getVbo());
		}
		hud.attachChild(dedicated2);

		if (dedicated3 == null)
		{
			dedicated3 = new Text(0, 0, fnt_Small, TEKS_DEDICATED_NAME3, getVbo());
		}
		hud.attachChild(dedicated3);

		if (version == null)
		{
		    version = new Text(0, 0, fnt_Medium, TEKS_VERSION, getVbo());
		}
		hud.attachChild(version);

		if (version_code == null)
		{
		    version_code = new Text(0, 0, fnt_Small, TEKS_VERSION_CODE, getVbo());
		}
		hud.attachChild(version_code);
		
		if (event == null)
		{
			event = new Text(0, 0, fnt_Medium, TEKS_EVENT, getVbo());
		}
		hud.attachChild(event);

		if (event_name1 == null)
		{
			event_name1 = new Text(0, 0, fnt_Small, TEKS_EVENT_NAME1, getVbo());
		}
		hud.attachChild(event_name1);

		if (event_name2 == null)
		{
			event_name2 = new Text(0, 0, fnt_Small, TEKS_EVENT_NAME2, getVbo());
		}
		hud.attachChild(event_name2);

		if (copyright == null)
		{
			copyright = new Text(0, 0, fnt_Medium, TEKS_COPYRIGHT, getVbo());
		}
		hud.attachChild(copyright);

		if (year == null)
		{
			year = new Text(0, 0, fnt_Small, TEKS_COPYRIGHT_YEAR, getVbo());
		}
		hud.attachChild(year);

		grafik.spr_Menu_Credits_Logo_Hantu.setPosition(setPosX(grafik.spr_Menu_Credits_Logo_Hantu), RES_HEIGHT);
		
		//project_man.setPosition(setPosX(project_man), Utils.getRatioH(100));
		project_man.setPosition(setPosX(project_man), setPosY(grafik.spr_Menu_Credits_Logo_Hantu, TEXT_MARGIN_2));
		project_man_name.setPosition(setPosX(project_man_name), setPosY(project_man, TEXT_MARGIN_1));

		producer.setPosition(setPosX(producer), setPosY(project_man_name, TEXT_MARGIN_2));
		producer_name1.setPosition(setPosX(producer_name1), setPosY(producer, TEXT_MARGIN_1));
		producer_name2.setPosition(setPosX(producer_name2), setPosY(producer_name1, TEXT_MARGIN_1));

		gamedesign.setPosition(setPosX(gamedesign), setPosY(producer_name2, TEXT_MARGIN_2));
		gamedesign_name1.setPosition(setPosX(gamedesign_name1), setPosY(gamedesign, TEXT_MARGIN_1));
		gamedesign_name2.setPosition(setPosX(gamedesign_name2), setPosY(gamedesign_name1, TEXT_MARGIN_1));
		gamedesign_name3.setPosition(setPosX(gamedesign_name3), setPosY(gamedesign_name2, 10));

		programmer.setPosition(setPosX(programmer), setPosY(gamedesign_name3, TEXT_MARGIN_2));
		programmer_name1.setPosition(setPosX(programmer_name1), setPosY(programmer, TEXT_MARGIN_1));
		programmer_name2.setPosition(setPosX(programmer_name2), setPosY(programmer_name1, TEXT_MARGIN_1));

		artist.setPosition(setPosX(artist), setPosY(programmer_name2, TEXT_MARGIN_2));
		artist_name1.setPosition(setPosX(artist_name1), setPosY(artist, TEXT_MARGIN_1));
		artist_name2.setPosition(setPosX(artist_name2), setPosY(artist_name1, TEXT_MARGIN_1));
		artist_name3.setPosition(setPosX(artist_name3), setPosY(artist_name2, TEXT_MARGIN_1));
		artist_name4.setPosition(setPosX(artist_name4), setPosY(artist_name3, TEXT_MARGIN_1));
		artist_name5.setPosition(setPosX(artist_name5), setPosY(artist_name4, TEXT_MARGIN_1));

		sound_eng.setPosition(setPosX(sound_eng), setPosY(artist_name5, TEXT_MARGIN_2));
		sound_eng_name1.setPosition(setPosX(sound_eng_name1), setPosY(sound_eng, TEXT_MARGIN_1));
		sound_eng_name2.setPosition(setPosX(sound_eng_name2), setPosY(sound_eng_name1, TEXT_MARGIN_1));

		dedicated.setPosition(setPosX(dedicated), setPosY(sound_eng_name2, TEXT_MARGIN_2));
		dedicated1.setPosition(setPosX(dedicated1), setPosY(dedicated, TEXT_MARGIN_1));
		dedicated2.setPosition(setPosX(dedicated2), setPosY(dedicated1, TEXT_MARGIN_1));
		dedicated3.setPosition(setPosX(dedicated3), setPosY(dedicated2, TEXT_MARGIN_1));

		version.setPosition(setPosX(version), setPosY(dedicated3, TEXT_MARGIN_2));
		version_code.setPosition(setPosX(version_code), setPosY(version, TEXT_MARGIN_1));
		
		grafik.spr_Menu_Credits_Logo_Agd.setPosition(setPosX(grafik.spr_Menu_Credits_Logo_Agd), setPosY(version_code, TEXT_MARGIN_2));
		grafik.spr_Menu_Credits_Logo_Amikom.setPosition(setPosX(grafik.spr_Menu_Credits_Logo_Amikom), setPosY(grafik.spr_Menu_Credits_Logo_Agd, TEXT_MARGIN_2));

		event.setPosition(setPosX(event), setPosY(grafik.spr_Menu_Credits_Logo_Amikom, TEXT_MARGIN_2));
	//	event.setPosition(setPosX(event), setPosY(dedicated3, TEXT_MARGIN_2));
		event_name1.setPosition(setPosX(event_name1), setPosY(event, TEXT_MARGIN_1));
		event_name2.setPosition(setPosX(event_name2), setPosY(event_name1, TEXT_MARGIN_1));

		copyright.setPosition(setPosX(copyright), setPosY(event_name2, TEXT_MARGIN_2));
		year.setPosition(setPosX(year), setPosY(copyright, TEXT_MARGIN_1));
	}
	
	public void attachRule(TiledSprite p_Bg)
	{
		txt_Rule = new Text(0
				, 0
				, fnt_Medium, "0"
				, "00".length()
				, getVbo());
		txt_Rule.setPosition((p_Bg.getWidth() - 5) * 2 - txt_Rule.getWidth()
			, (p_Bg.getHeight() - txt_Rule.getHeight()) / 2);
		    p_Bg.attachChild(txt_Rule);
		    lst_Rule.add(txt_Rule);
	}
	
	public void detachRule()
	{
	    	while (lst_Rule.size() != 0)
		{
	    	    lst_Rule.get(lst_Rule.size() - 1).detachSelf();
		    lst_Rule.remove(lst_Rule.size() - 1);
		}
	}

	private float setPosY(Text txt, float angka)
	{
		return txt.getY() + txt.getHeight() + Utils.getRatioH(angka);
	}

	private float setPosX(Text txt)
	{
		return (RES_WIDTH / 2) - (txt.getWidth() / 2);
	}
	
	private float setPosY(Sprite spr, float angka)
	{
		return spr.getY() + spr.getHeight() + Utils.getRatioH(angka);
	}

	private float setPosX(Sprite spr)
	{
		return (RES_WIDTH / 2) - (spr.getWidth() / 2);
	}

	public void setVisible(Boolean bol)
	{
		project_man.setVisible(bol);
		project_man_name.setVisible(bol);

		producer.setVisible(bol);
		producer_name1.setVisible(bol);
		producer_name2.setVisible(bol);

		gamedesign.setVisible(bol);
		gamedesign_name1.setVisible(bol);
		gamedesign_name2.setVisible(bol);
		gamedesign_name3.setVisible(bol);

		programmer.setVisible(bol);
		programmer_name1.setVisible(bol);
		programmer_name2.setVisible(bol);

		artist.setVisible(bol);
		artist_name1.setVisible(bol);
		artist_name2.setVisible(bol);
		artist_name3.setVisible(bol);
		artist_name4.setVisible(bol);
		artist_name5.setVisible(bol);

		sound_eng.setVisible(bol);
		sound_eng_name1.setVisible(bol);
		sound_eng_name2.setVisible(bol);

		dedicated.setVisible(bol);
		dedicated1.setVisible(bol);
		dedicated2.setVisible(bol);
		dedicated3.setVisible(bol);

		version.setVisible(bol);
		version_code.setVisible(bol);
		
		event.setVisible(bol);
		event_name1.setVisible(bol);
		event_name2.setVisible(bol);

		copyright.setVisible(bol);
		year.setVisible(bol);

		grafik.spr_Menu_Credits_Logo_Agd.setVisible(bol);
		grafik.spr_Menu_Credits_Logo_Amikom.setVisible(bol);
		grafik.spr_Menu_Credits_Logo_Hantu.setVisible(bol);	

	}
	
	public void attachMenuStage()
	{
	    txt_Menu_Stage	= new Text(0, 0, fnt_Medium, TXT_SELECT_STAGE, getVbo());
	    txt_Menu_Stage.setPosition(RES_WIDTH + (RES_WIDTH - txt_Menu_Stage.getWidth()) / 2,0);
	    txt_Menu_Stage.setColor(Color.WHITE);
	    
	    m_Scene.attachChild(txt_Menu_Stage);
	}
	
	public void detachMenuStage()
	{
	    txt_Menu_Stage.detachSelf();
	}
	
	public void attachMenuLevel()
	{
	    txt_Menu_Level	= new Text(0, 0, fnt_Medium, TXT_SELECT_LEVEL, getVbo());
	    txt_Menu_Level.setPosition((RES_WIDTH - txt_Menu_Stage.getWidth()) / 2,0);
	    txt_Menu_Level.setColor(Color.WHITE);
	    
	    m_Scene.attachChild(txt_Menu_Level);
	}
	
	public void detachMenuLevel()
	{
	    txt_Menu_Level.detachSelf();
	}
	
	public void attachClue(Rectangle rectangle)
	{
	    txt_Clue	= new Text(0, 0, fnt_Small, "Need Clue ?", getVbo());
	    txt_Clue.setPosition((RES_WIDTH - txt_Clue.getWidth()) / 2,
		    RES_HEIGHT - Utils.getRatio(100));
	    rectangle.attachChild(txt_Clue);
	}
	
	public void detachClue()
	{
	    txt_Clue.detachSelf();
	}

}
